package com.baixun.gassystem.service.impl;

import com.baixun.gasentity.dto.AlgorithmCallbackRequest;
import com.baixun.gasentity.entity.AnalysisResult;
import com.baixun.gasentity.entity.Point;
import com.baixun.gasentity.entity.SysDevice;
import com.baixun.gassystem.mapper.AnalysisResultMapper;
import com.baixun.gassystem.mapper.DeviceMapper;
import com.baixun.gassystem.mapper.PointMapper;
import com.baixun.gassystem.service.AlgorithmService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;

/**
 * @program: 上海燃气驾驶舱
 * @description: 事件回调接口【数据保存到数据库】实现类
 * @author: yesong
 **/
@Service
public class AlgorithmServiceImpl implements AlgorithmService {

    @Autowired
    private PointMapper pointMapper;

//    @Resource
//    private DeviceMapper deviceMapper;

    @Autowired
    private AnalysisResultMapper analysisResultMapper;

    @Override
    public void processAlgorithmCallback(AlgorithmCallbackRequest request) {
        // 保存 Point 信息
        // 先检查点位ID是否存在
        Long pointId = request.getPointInfo().getPointId();
        Point existingPoint = pointMapper.findPointById(pointId);

        if (existingPoint != null) {
            // 如果存在，则更新现有记录
            existingPoint.setDepartmentId(request.getPointInfo().getDepartmentId());
            existingPoint.setDepartmentPath(request.getPointInfo().getDepartmentPath());
            existingPoint.setExtraInfo(request.getPointInfo().getExtraInfo());
            pointMapper.updatePoint(existingPoint);
        } else {
            // 如果不存在，则新增记录
            Point point = new Point();
            point.setPointId(pointId); // 设置点位ID
            point.setDepartmentId(request.getPointInfo().getDepartmentId());
            point.setDepartmentPath(request.getPointInfo().getDepartmentPath());
            point.setExtraInfo(request.getPointInfo().getExtraInfo());
            pointMapper.insertPoint(point);
        }

//        // 保存 Device 信息
//        SysDevice device = new SysDevice();
//        device.setDeviceId(request.getDeviceInfo().getDeviceId());
//        device.setDeviceName(request.getDeviceInfo().getDeviceName());
//        device.setExtraInfo(request.getDeviceInfo().getExtraInfo());
//        deviceMapper.insertDevice(device);

        // 保存 AnalysisResult 信息
        AnalysisResult analysisResult = new AnalysisResult();
        analysisResult.setPointId(request.getPointInfo().getPointId());
        analysisResult.setEventType(request.getAnalysisResult().getEvent().get(0).getEventType());
        analysisResult.setEventId(request.getAnalysisResult().getEvent().get(0).getEventId());
        analysisResult.setEventStatus(request.getAnalysisResult().getEvent().get(0).getEventStatus());
        analysisResult.setCaptureTime(request.getAnalysisResult().getEvent().get(0).getCaptureTime());
        analysisResult.setSnapshotUrl(request.getAnalysisResult().getEvent().get(0).getSnapshot().getUrl());
        analysisResult.setSnapshotWidth(request.getAnalysisResult().getEvent().get(0).getSnapshot().getWidth());
        analysisResult.setSnapshotHeight(request.getAnalysisResult().getEvent().get(0).getSnapshot().getHight());

        analysisResult.setLabel(request.getAnalysisResult().getEvent().get(0).getResult().getItems().get(0).getLabel());
        analysisResult.setCropSnapshotUrl(request.getAnalysisResult().getEvent().get(0).getResult().getItems().get(0).getCropSnapshotUrl());
        analysisResult.setScore(request.getAnalysisResult().getEvent().get(0).getResult().getItems().get(0).getScore());
        analysisResult.setLocationLeft(request.getAnalysisResult().getEvent().get(0).getResult().getItems().get(0).getLocation().getLeft());
        analysisResult.setLocationTop(request.getAnalysisResult().getEvent().get(0).getResult().getItems().get(0).getLocation().getTop());
        analysisResult.setLocationWidth(request.getAnalysisResult().getEvent().get(0).getResult().getItems().get(0).getLocation().getWidth());
        analysisResult.setLocationHeight(request.getAnalysisResult().getEvent().get(0).getResult().getItems().get(0).getLocation().getHeight());

        analysisResult.setVideoUrl(request.getAnalysisResult().getVideoUrl());
        analysisResult.setDetectAreaType(request.getAnalysisResult().getDetectAreas().get(0).getType());
        analysisResult.setDetectAreaCoordinates(new Gson().toJson(request.getAnalysisResult().getDetectAreas().get(0).getSides()));

        analysisResult.setCreateTime(new Timestamp(System.currentTimeMillis()));
        analysisResult.setDeleted(0);

        // 保存分析结果
        analysisResultMapper.insertAnalysisResult(analysisResult);
    }
}

